package com.mars.component.scheduler.service.impl;

import org.quartz.Job;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mars.component.scheduler.model.SchedulerJob;
import com.mars.component.scheduler.quartz.SchedulerUtils;
import com.mars.component.scheduler.service.SchedulerService;

@Service
public class SchedulerServiceImpl implements SchedulerService {

	@Autowired
	private SchedulerUtils schedulerUtils ;
	
	@Override
	public void createScheduleJob(Class<? extends Job> clazz, SchedulerJob schedulerJob) {
		try {
			this.schedulerUtils.createScheduleJob(clazz, schedulerJob);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void createScheduleJob(SchedulerJob schedulerJob) {
		try {
			this.schedulerUtils.createScheduleJob(schedulerJob);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void rescheduleJob(SchedulerJob schedulerJob) {
		try {
			this.schedulerUtils.rescheduleJob(schedulerJob);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void pauseJob(String[] jobIds, String[] groups) {
		try {
			for(int i=0; i<jobIds.length; i++) {
				this.schedulerUtils.pauseJob(jobIds[i], groups[i]);
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void pauseJob(String jobId, String group) {
		this.pauseJob(new String[]{jobId}, new String[]{group});
	}

	@Override
	public void resumeJob(String[] jobIds, String[] groups) {
		try {
			for(int i=0; i<jobIds.length; i++) {
				this.schedulerUtils.resumeJob(jobIds[i], groups[i]);
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void resumeJob(String jobId, String group) {
		this.resumeJob(new String[]{jobId}, new String[]{group});
	}

	@Override
	public void deleteJob(String[] jobIds, String[] groups) {
		try {
			for(int i=0; i<jobIds.length; i++) {
				this.schedulerUtils.deleteJob(jobIds[i], groups[i]);
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteJob(String jobId, String group) {
		this.deleteJob(new String[]{jobId}, new String[]{group});
	}

	@Override
	public void runNowJob(String[] jobIds, String[] groups) {
		try {
			for(int i=0; i<jobIds.length; i++) {
				this.schedulerUtils.runNow(jobIds[i], groups[i]);
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void runNowJob(String jobId, String group) {
		this.runNowJob(new String[]{jobId}, new String[]{group});
	}

	@Override
	public boolean checkExists(String jobId, String group) {
		return this.checkExists(jobId, group);
	}

}
